package com.boarsoft.boar.batch.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.boarsoft.boar.batch.bean.PageInfo;
import com.boarsoft.boar.batch.biz.OrdTblPageBiz;
import com.boarsoft.flow.core.bean.ForkHandler;

/**
 * 
 * @author Mac_J
 *
 */
public class OrdTblPageForkH implements ForkHandler {
	private OrdTblPageBiz biz;
	private String orderedField = "id";
	private int pageSize = 5000;
	private String totalQuery;
	private String pageQuery;

	@Override
	public Map<String, Object> fork(String key, Object data, Throwable throwable) {
		Map<String, Object> map = new HashMap<>();
		// 统计总数量
		long total = biz.getTotal(totalQuery);
		// 计算分页数 pageCount
		long pc = (total + pageSize - 1) / pageSize;
		// 依次查询每个分页，取该分页的分页排序字段的起始值
		for (int pn = 1; pn <= pc; pn++) {
			List<Object> lt = biz.list(pageQuery, pn, pageSize);
			PageInfo p = new PageInfo(pn, lt.get(0), lt.get(lt.size() - 1));
			map.put(p.toString(), p);
		}
		return map;
	}

	public String getOrderedField() {
		return orderedField;
	}

	public void setOrderedField(String orderedField) {
		this.orderedField = orderedField;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getTotalQuery() {
		return totalQuery;
	}

	public void setTotalQuery(String totalQuery) {
		this.totalQuery = totalQuery;
	}

	public String getPageQuery() {
		return pageQuery;
	}

	public void setPageQuery(String pageQuery) {
		this.pageQuery = pageQuery;
	}

	public OrdTblPageBiz getBiz() {
		return biz;
	}

	public void setBiz(OrdTblPageBiz biz) {
		this.biz = biz;
	}

}
